// console.log( '构建生产环境的包' )
// 要求：优化项目
let {task,src,dest,watch,series,parallel} = require('gulp')
let load = require('gulp-load-plugins')()
let del = require('del')

// 删除dist目录
task('del',async ()=>{
    del('./dist')
})

// 拷贝图片
task('imgs',async ()=>{
    src('./imgs/*.*')
    .pipe(dest('./dist/imgs'))
})

// 处理html
task('html',async ()=>{
    setTimeout(function (){
        src(['./rev/**/*.json','./view/*.html'])
        .pipe(load.revCollector({replaceReved: true}))//用带哈希值的路径替换原来的路径
        .pipe(load.minifyHtml())
        .pipe(dest('./dist'))
    },4000)
})

// 拷贝font
task('font',async ()=>{
    src('./font/*.*')
    .pipe(dest('./dist/font'))
})

// 拷贝lib
task('lib',async ()=>{
    src('./script/lib/*.*')
    .pipe(dest('./dist/js/lib'))
})

// 编译sass
task('sass',async ()=>{
    src('./sass/*.scss')
    .pipe(load.sassChina())//转成css
    .pipe(load.rev())//文件名添加哈希值
    .pipe(load.minifyCss())//压缩css
    .pipe(dest('./dist/css'))//输出到指定目录
    .pipe(load.rev.manifest())//生成保存哈希值的json文件
    .pipe(dest('./rev/css'))//将json文件保存到指定目录
})

// 处理js
task('script',async ()=>{
    src('./script/*.js')
    .pipe(load.babel({presets: ['@babel/env']}))//转成ES5
    .pipe(load.uglify())//压缩js
    .pipe(load.rev())//文件名添加哈希值
    .pipe(dest('./dist/js'))//输出到指定目录
    .pipe(load.rev.manifest())//生成保存哈希值的json文件
    .pipe(dest('./rev/js'))//将json文件保存到指定目录
})

// 打包
task('build',series('del','imgs','font','lib','sass','script','html'))
